package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsDwk;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyRsDwkMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsDwkService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-单位库
 * @Author: yuncheng
 * @Date: 2024-01-05
 * @Version: V1.0
 */
@Service
public class SpcyRsDwkServiceImpl extends BaseServiceImpl<SpcyRsDwkMapper, SpcyRsDwk> implements ISpcyRsDwkService {

    @Resource
    private SpcyRsDwkMapper spcyRsDwkMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;


    @Override
    public IPage<SpcyRsDwk> selectPageList(SpcyRsDwk query, String gjz, PageVo pageVo, OrderItemVo orderItemVo) {

        LambdaQueryWrapper<SpcyRsDwk> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(gjz)) {
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwdz()), SpcyRsDwk::getFdwdz, query.getFdwdz());
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwlx()), SpcyRsDwk::getFdwlx, query.getFdwlx());
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwqc()), SpcyRsDwk::getFdwqc, query.getFdwqc());
            queryWrapper.like(StringUtils.isNotBlank(query.getFssds()), SpcyRsDwk::getFssds, query.getFssds());
            queryWrapper.like(StringUtils.isNotBlank(query.getFsfyjs()), SpcyRsDwk::getFsfyjs, query.getFsfyjs());
        }else {
            queryWrapper
                    .and(f -> f.like(SpcyRsDwk::getFdwqc, gjz)
                            .or()
                            .like(SpcyRsDwk::getFdwdz, gjz)
                            .or()
                            .like(SpcyRsDwk::getFdwlx, gjz)
                            .or()
                            .like(SpcyRsDwk::getFssds, gjz)
                    );
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByAsc(SpcyRsDwk::getForder);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        Page<SpcyRsDwk> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        return spcyRsDwkMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SpcyRsDwk> selectList(SpcyRsDwk query, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SpcyRsDwk> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwdz()), SpcyRsDwk::getFdwdz, query.getFdwdz());
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwlx()), SpcyRsDwk::getFdwlx, query.getFdwlx());
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwqc()), SpcyRsDwk::getFdwqc, query.getFdwqc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFssds()), SpcyRsDwk::getFssds, query.getFssds());
        queryWrapper.like(StringUtils.isNotBlank(query.getFsfyjs()), SpcyRsDwk::getFsfyjs, query.getFsfyjs());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByAsc(SpcyRsDwk::getForder);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return spcyRsDwkMapper.selectList(queryWrapper);
    }

    @Override
    public List<SpcyRsDwk> selectList(SpcyRsDwk query, String gjz, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SpcyRsDwk> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(gjz)) {
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwdz()), SpcyRsDwk::getFdwdz, query.getFdwdz());
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwlx()), SpcyRsDwk::getFdwlx, query.getFdwlx());
            queryWrapper.like(StringUtils.isNotBlank(query.getFdwqc()), SpcyRsDwk::getFdwqc, query.getFdwqc());
            queryWrapper.like(StringUtils.isNotBlank(query.getFssds()), SpcyRsDwk::getFssds, query.getFssds());
            queryWrapper.like(StringUtils.isNotBlank(query.getFsfyjs()), SpcyRsDwk::getFsfyjs, query.getFsfyjs());
        }else {
            queryWrapper
                    .and(f -> f.like(SpcyRsDwk::getFdwqc, gjz)
                            .or()
                            .like(SpcyRsDwk::getFdwdz, gjz)
                            .or()
                            .like(SpcyRsDwk::getFdwlx, gjz)
                            .or()
                            .like(SpcyRsDwk::getFssds, gjz)
                    );

        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByAsc(SpcyRsDwk::getForder);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return spcyRsDwkMapper.selectList(queryWrapper);
    }

    @Override
    public List<SpcyRsDwk> selectList(SpcyRsDwk query) {
        LambdaQueryWrapper<SpcyRsDwk> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwdz()), SpcyRsDwk::getFdwdz, query.getFdwdz());
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwlx()), SpcyRsDwk::getFdwlx, query.getFdwlx());
        queryWrapper.like(StringUtils.isNotBlank(query.getFdwqc()), SpcyRsDwk::getFdwqc, query.getFdwqc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFssds()), SpcyRsDwk::getFssds, query.getFssds());
        queryWrapper.like(StringUtils.isNotBlank(query.getFsfyjs()), SpcyRsDwk::getFsfyjs, query.getFsfyjs());
        return spcyRsDwkMapper.selectList(queryWrapper);
    }

    @Override
    public List<SpcyRsDwk> verifyDwIsExist(String fdwqc) {
        return spcyRsDwkMapper.selectList(new LambdaQueryWrapper<SpcyRsDwk>()
                .eq(SpcyRsDwk::getFdwqc, fdwqc));
    }

    @Override
    public SpcyRsDwk addDw(SpcyRsDwk bean) {
        bean.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(bean.getFsfyjs())){
            bean.setFsfyjs("否");
        }

        verifyEmpty(bean, true);

        int order = selectList(new SpcyRsDwk()).size()+1;
        bean.setForder(order);
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setCreateTime(new Date());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateTime(new Date());
        spcyRsDwkMapper.insert(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_RS_DWGL,operContent,bean.getId(), "添加单位", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updatwDw(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.get("id");
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }
        SpcyRsDwk updateBean = spcyRsDwkMapper.selectById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        String oldDw = updateBean.getFdwqc();

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        updateBean.setUpdateTime(new Date());

        verifyEmpty(updateBean, false);

        if (StringUtils.isNotBlank(oldDw) && !oldDw.equals(updateBean.getFdwqc())) {
            verifyEmpty(updateBean, true);
        }

        spcyRsDwkMapper.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_RS_DWGL,operContent,id,"修改单位",SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public void deleteDw(String id) {
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }
        SpcyRsDwk bean = spcyRsDwkMapper.selectById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        spcyRsDwkMapper.deleteById(id);

        String operContent = "删除信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_RS_DWGL,operContent,bean.getId(), "删除单位", SpcyConstant.LOGS_SYS_TYPE_SC);

    }

    /** 验证空值 */
    private void verifyEmpty(SpcyRsDwk bean, boolean isAdd){
        if (StringUtils.isBlank(bean.getFdwqc())){
            throw new BusinessException("单位名称为空！");
        }
        if (StringUtils.isBlank(bean.getFdwdz())){
            throw new BusinessException("单位地址为空！");
        }
        if (StringUtils.isBlank(bean.getFdwlx())){
            throw new BusinessException("单位类型为空！");
        }
        if (StringUtils.isBlank(bean.getFssds())){
            throw new BusinessException("单位地市为空！");
        }

        if (isAdd) {
            List<SpcyRsDwk> dwList = verifyDwIsExist(bean.getFdwqc());
            if (CollectionUtil.isNotEmpty(dwList)) {
                throw new BusinessException("该单位已经存在了！");
            }
        }
    }
}
